import Vue from 'vue'
import VueRouter from 'vue-router'
import jwt_decode from 'jwt-decode'
import { Message } from 'element-ui';
Vue.use(VueRouter)


const router = new VueRouter({
  mode: 'hash',
  routes: [
    { path: '/', redirect: '/admin' },
    { path: '/login', name: 'Login', component: () => import('@/components/menu/Login.vue') },
    /*  { path: '/', name: 'upload', component: () => import('@/components/Upload.vue') }, */
    //管理员界面
    {
      path: '/admin', component: () => import('@/components/menu/Admin.vue'),
      children: [
        { path: '/admin/student', component: () => import('@/components/main/studentAdmin.vue') },
        { path: '/admin/teacher', component: () => import('@/components/main/teacherAdmin.vue') },
        { path: '', component: () => import('@/components/homepage/homepage.vue') },
        { path: '/admin/noticeSend', component: () => import('@/components/main/noticeSend.vue') },
        { path: '/admin/otp', component: () => import('@/components/socket/otp_socket.vue') },
        { path: '/admin/oto', component: () => import('@/components/socket/otp_socket.vue') },
        { path: '/admin/add', component: () => import('@/components/main/addclass.vue') },
      ]
    },
    {
      path: '/student', name: 'Student', component: () => import('@/components/menu/Student.vue'),
      children: [
        { path: '', component: () => import('@/components/homepage/homepage.vue') },
        { path: '/student/myNotice', component: () => import('@/components/main/myNotice.vue') },
        { path: '/student/health', component: () => import('@/components/main/healthTable.vue') },
        { path: '/student/application', component: () => import('@/components/main/application.vue') },
        { path: '/student/user', component: () => import('@/components/main/user.vue') },
        { path: '/student/otp', component: () => import('@/components/socket/otp_socket.vue') },
        { path: '/student/oto', component: () => import('@/components/socket/otp_socket.vue') },
      ]
    },
    {
      path: '/teacher', name: 'Teacher', component: () => import('@/components/menu/Teacher.vue'),
      children: [
        { path: '', component: () => import('@/components/homepage/homepage.vue') },
        { path: 'notice', component: () => import('@/components/main/noticeSend.vue') },
        { path: 'leave', component: () => import('@/components/main/leave.vue') },
        { path: 'user', component: () => import('@/components/main/user.vue') },
        { path: 'otp', component: () => import('@/components/socket/otp_socket.vue') },
        { path: 'oto', component: () => import('@/components/socket/otp_socket.vue') },
      ]
    },
  ]
})
//路由守卫 
router.beforeEach(async (to, from, next) => {
  const isLogin = localStorage.eleToken ? true : false
  if (to.path == '/login' || to.path == '/register') {
    next()
  } else {
    if (!isLogin) {
      next('/login')
    }
    const decode = jwt_decode(localStorage.eleToken)
    if (decode.type == 1) {
      next()
    } else if (decode.type == 2) {
      if (to.path == '/student' ||
        to.path == '/student/oto' ||
        to.path == '/student/otp' ||
        to.path == '/student/user' ||
        to.path == '/student/application' ||
        to.path == '/student/health' ||
        to.path == '/student/myNotice') {
        next()
      } else {
        next('/student')
        Message.warning('您不可以访问该页面')
      }
    } else {
      if (to.path == '/teacher' ||
        to.path == '/teacher/oto' ||
        to.path == '/teacher/otp' ||
        to.path == '/teacher/user' ||
        to.path == '/teacher/leave' ||
        to.path == '/teacher/notice') {
        next()
      } else {
        next('/teacher')
        Message.warning('您不可以访问该页面')
      }
    }
  }
})
export default router
